"""
登录
"""
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import time
from conf import settings
from lib.common import wait_table
from interface.init_interface import config_dict
import logging.config

logging.config.dictConfig(settings.LOGGING_DIC)  # 导入上面定义的logging配置
logger = logging.getLogger("登录")  # 生成一个log实例


def login_interface(browser, wait):
    """
    4A登录
    """
    USER = config_dict.get('WEB').get('USER')
    PASSWORD = config_dict.get('WEB').get('PASSWORD')

    loginName_element = browser.find_element(By.ID, 'loginName')
    password_element = browser.find_element(By.ID, 'password')

    for i in 'admin':
        loginName_element.send_keys(Keys.BACKSPACE)

    loginName_element.send_keys(USER)
    password_element.send_keys(PASSWORD)

    browser.find_element(By.ID, 'login_btn').click()

    logger.info(f"用户 {USER} 登录成功 ")

    # 设备管理
    wait.until(EC.element_to_be_clickable((
        By.XPATH, '//*[@id="IndexMenuUL"]/li[3]/table/tbody/tr/td[2]'
    ))).click()
    # 子iframe切换
    browser.switch_to.frame('LT_main')

    # 等待加载
    wait_table(browser)
